package BluetoothFileMgr.start;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

public class FileSocket implements Runnable{
	
	private BluetoothDevice device = null;
	private final Context context = null;
	//private final BluetoothAdapter adapter;
	public static BluetoothSocket fileSock = null;
	DataInputStream din;// = ConnectThread.din;
	String filename = null;
	long size = 0;
	//DeviceConnect dc;
	
	//int pos;
	 
	    public FileSocket(String filename, BluetoothDevice device, long size)
	    {
	        //mmSocket = DeviceConnect.mmSocket;
	        //device = DeviceConnect.device;
	    	this.device = DeviceConnect.device; 
	        this.filename = filename;
	        this.size = size;
	        din = DeviceConnect.din;
	        //this.dc = dc;
	        //pos = position;
	        //this.s = s;
	    }
	 
	    /*Handler hand = new Handler()
        {  	public void handleMessage(Message msg)
        	{
          			//Toast toast = Toast.makeText(FileManager.fileManCon, (msg.obj).toString(), Toast.LENGTH_LONG);
        			//toast.show();
        			//FileManager.tv2.setText((msg.obj).toString());
        			
           	}
        };*/
	    
	    public void run()
	    {
	    	//try
	    	//{	Message msg = hand.obtainMessage();
	    	//	msg.obj = "in run";
	    	//	//hand.sendMessage(msg);
	    	//} catch (Exception e)
	    	//{	Message msg = hand.obtainMessage();
	    	//	msg.obj = "in catch of run";
	    	//	//hand.sendMessage(msg);
	    	//}
	    	trial(1);
	    }
	    
	    /*public void getFile() throws IOException
	    {
	    	DataInputStream fin = null;
			try {
				fin = new DataInputStream(fileSock.getInputStream());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            
            {	DataOutputStream fout = null;
            	FileOutputStream f = new FileOutputStream(Environment.getExternalStorageDirectory().toString()+"/"+filename);
            	fout = new DataOutputStream(f);
            	//String st = sc.nextLine();
            	String st = null;
            	FileManager.tv2.setText("before readUTF");
            	st = fin.readUTF();
            	FileManager.tv2.setText("after readUTF n before writeUTF");
            	//if(!st.contains("::"))
            	fout.writeUTF(st);
            	FileManager.tv2.setText("after writeUTF");
            	//FileManager.dirs.clear();
            	//FileManager.type.clear();
            }
	    }*/


	    public void trial(int position) //throws Exception
	    {
	    	/*BluetoothSocket fileSock = null;
	    	BluetoothSocket tmp = null;
	    	try {
	    		//FileManager.tv2.setText("in try for socket");
	    		
	    		//fileSock = DeviceConnect.mmSocket;
	    		//fileSock = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FC"));
	    		//tv.setText("before conn");
	    		try
		    	{	Message msg = hand.obtainMessage();
		    		msg.obj = "in before conn";
		    		//hand.sendMessage(msg);
		    	} catch (Exception e)
		    	{	Message msg = hand.obtainMessage();
		    		msg.obj = "in catch of before conn";
		    		//hand.sendMessage(msg);
		    	}
	    		//fileSock.connect();
	    		//tv.setText("after conn");
	    		try
		    	{	Message msg = hand.obtainMessage();
		    		msg.obj = "in AFTER conn";
		    		//hand.sendMessage(msg);
		    	} catch (Exception e)
		    	{	Message msg = hand.obtainMessage();
		    		msg.obj = "in catch of before conn";
		    		//hand.sendMessage(msg);
		    	}
	    	} catch (Exception e) {
	    		try
		    	{	Message msg = hand.obtainMessage();
		    		msg.obj = e.getMessage().toString();
		    		//hand.sendMessage(msg);
		    	} catch (Exception e2)
		    	{	Message msg = hand.obtainMessage();
		    		msg.obj = "in catch of before conn";
		    		//hand.sendMessage(msg);
		    	}
	    	}//tv.setText("connect--"+e.toString()); }
	    	//fileSock = tmp;
	    	*/
	    	File newFile = null;
	    	FileInputStream fin = null;
	    	try {
	    		//tv.setText("before");
	    		newFile = new File(Environment.getExternalStorageDirectory().toString()+"/"+filename);
	    		newFile.createNewFile();
	    	} catch (Exception e) {
	    		// TODO Auto-generated catch block
	    		//tv.setText(e.toString());
	    		e.printStackTrace();
	    	}
	    	
	    	//DataInputStream dins = null;
	    	//DataInputStream din = ConnectThread.din;;
	    	//try {
				//dins = new DataInputStream(DeviceConnect.mmSocket.getInputStream());
			//} catch (IOException e1) {
				// TODO Auto-generated catch block
				//e1.printStackTrace();
			//}
	    	//InputStream inst = null;
	    	//try {
	    	//	inst = DeviceConnect.mmSocket.getInputStream();
	    	//} catch (IOException e){}
	    	//int i = 0;
	    	//while(true)
	    	///////////////////////////////////////////////////////////////
	    			
	    		DataOutputStream dout = null;
	    		FileOutputStream fout = null;
	    		try {
	    			fout = new FileOutputStream(newFile);
	    			dout = new DataOutputStream(fout);
	    			//fout.close();
	    			
	    		} catch (FileNotFoundException e) { } 	                   	
	    		String st = null;
	    		byte b[] = new byte[1000];
	    		byte by;// = new byte();
	    		//FileManager.tv2.setText("before readUTF");
	    		try { 			
	    			long i = 1;//pdf=246000
	    			while(i<=size)
	    			{
	    				//tv.setText(""+i);
	    				b = new byte[1000];
	    				din.read(b, 0, 1);
	    				by = b[0];
	    				dout.write(by);
	    				//System.out.print(by);
	    				i++;
	    				//Message msg = hand.obtainMessage();
	        			//msg.obj = by+"READing"+i;
	        			//hand.sendMessage(msg);
	    				//b = new byte[1000];
	    			}
	    			//tv.setText("read");
	    			//inst.close(); 
	    			//Message msg = hand.obtainMessage();
        			//msg.obj = "READ";
        			//hand.sendMessage(msg);
	    		} catch (Exception e) {
	    			// TODO Auto-generated catch block
	    			//tv.setText("catch of inst.read");
	    		}
	    	
	    		//FileManager.tv2.setText("after readUTF n before writeUTF");
	    		//if(!st.contains("::"))
	    		try {
	    			//fout.writeUTF(st);
	    			//dout.write(b);
	    			fout.close();
	    			dout.close();
	    		} catch (Exception e) { }//tv.setText("catch of dout.write"); }
	    		//FileManager.tv2.setText("after writeUTF");
	    		//FileManager.dirs.clear();
	    		//FileManager.type.clear();
	    		
	    	////
	    	
	    }
}
